
layui.use(['table', 'admin', 'ax', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;

    /**
     * Customer Management
     * 客户表管理
     */
    var BasicCustomer = {
        tableId: "basicCustomerTable"
    };

    /**
     * 初始化表格的列
     */
    BasicCustomer.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'ID'},
            {align: 'center', toolbar: '#tableBar', title: 'Actions',width:120}, // 操作
            {field: 'status', sort: true, title: 'Status', templet: '#statusTpl'}, // 状态
            {field: 'customerOperateName', title: 'Project'}, // 项目
            {field: 'customerName', title: 'Name',width:200}, // 名称
            {field: 'customerNumber', title: 'Code'}, // 编号
            {field: 'intercepts', title: 'Outbound Import Cut Production Date'}, // 出库导入截取生产日期
            {field: 'occupy', title: 'Outbound Remove Occupied Inventory'}, // 出库是否去除占用库存
            {field: 'binding', title: 'Goods Bind Location for Storage'}, // 商品是否按照绑定库位入库
            {field: 'outorderconfirm', title: 'Outbound Order Confirmation'}, // 是否出库订单确认

            {field: 'justAppkey', title: 'Jushuitan appKey'},
            {field: 'justAppsecret', title: 'Jushuitan appSecret'},
            {field: 'accessToken', title: 'Jushuitan Customer Token'},
            {field: 'accessTokenme', title: 'Jushuitan Our Token'},
            {field: 'ownercoId', title: 'Jushuitan Owner Code'},

            {field: 'storeIdXlb', title: 'XinLingBang Store ID'},
            {field: 'gsnumber', title: 'XinLingBang Company No.'},
            {field: 'xlbStorehouseId', title: 'XinLingBang Warehouse ID'},
            {field: 'pullStoragetaskXlb', sort: true, title: 'Auto Pull XinLingBang Picking Task', templet: '#pullStoragetaskXlbTpl'}, // 自动拉取新零帮拣货任务
            {field: 'pullYuyueXlb', sort: true, title: 'Auto Pull XinLingBang Storage Request', templet: '#pullYuyueXlbTpl'}, // 自动拉取新零帮入库申请单
            {field: 'pullReturnXlb', sort: true, title: 'Auto Pull XinLingBang Store Return', templet: '#pullReturnXlbTpl'}, // 自动拉取新零帮门店退货单
            {field: 'inWarehousing', sort: true, title: 'Auto Recommend Storage Location', templet: '#inWarehousingTpl'}, // 是否自动推荐入库库位
            {field: 'needCheck', sort: true, title: 'Need Quality Inspection', templet: '#needCheckTpl'}, // 是否质检
            {field: 'generateTms', sort: true, title: 'Generate TMS Order', templet: '#generateTmsTpl'}, // 是否生成货运订单
            {field: 'agvRobot', sort: true, title: 'AGV Robot'},
            {field: 'agvTaskRunFlag', sort: true, title: 'Task Run Limitation'},
            {field: 'customerTerm', title: 'Cooperation Term', hide: true},
            {field: 'customerSettlement', title: 'Settlement Unit', hide: true},
            {field: 'customerUno', title: 'Customer Abbreviation', hide: true},
            {field: 'customerAssistcode', title: 'Mnemonic Code', hide: true},
            {field: 'customerContacts', title: 'Contact Person', hide: true},
            {field: 'customerFax', title: 'Fax', hide: true},
            {field: 'customerTel', title: 'Phone', hide: true},
            {field: 'customerEmall', title: 'Email', hide: true},
            {field: 'customerWebsite', title: 'Website', hide: true},
            {field: 'customerProvinces', title: 'Province', hide: true},
            {field: 'customerCiywhere', title: 'City', hide: true},
            {field: 'customerRange', title: 'Business Scope', hide: true},
            {field: 'customerIndustry', title: 'Industry Type', hide: true},
            {field: 'customerLevel', title: 'Customer Level', hide: true},
            {field: 'customerCreditrank', title: 'Credit Rating', hide: true},
            {field: 'customerOpeningplace', title: 'Account Opening Place', hide: true},
            {field: 'customerBank', title: 'Bank', hide: true},
            {field: 'customerBankaccount', title: 'Bank Account', hide: true},
            {field: 'customerLicense', title: 'Business License', hide: true},
            {field: 'customerTaxationcard', title: 'Tax Registration', hide: true},
            {field: 'customerFactorycode', title: 'Factory Code', hide: true},
            {field: 'customerAddress', title: 'Detailed Address', hide: true},
            {field: 'customerCreditcode', title: 'Unified Social Credit Code', hide: true},
            {field: 'createTime', title: 'Creation Time'}, // 创建时间
        ]];
    };

    /**
     * 点击查询按钮
     */
    BasicCustomer.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['customerOperateId'] = $("#customerOperateId").val();
        table.reload(BasicCustomer.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * Open Add Dialog
     * 弹出添加对话框
     */
    BasicCustomer.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/basicCustomer/add';
    };

    /**
     * 导出excel按钮 - Export Excel button
     */
    BasicCustomer.exportExcel = function () {
        var checkRows = table.checkStatus(BasicCustomer.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("Please select data to export"); // 请选择要导出的数据
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * Click Edit
     * 点击编辑
     *
     * @param data Row data when clicking the button
     */
    BasicCustomer.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/basicCustomer/edit?id=' + data.id;
    };

    /**
     * Click Delete
     * 点击删除
     *
     * @param data Row data when clicking the button
     */
    BasicCustomer.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/basicCustomer/delete", function (data) {
                Feng.success("Delete successful!"); // 删除成功!
                table.reload(BasicCustomer.tableId);
            }, function (data) {
                Feng.error("Delete failed!" + data.responseJSON.message + "!"); // 删除失败!
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("Confirm deletion?", operation); // 是否删除?
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BasicCustomer.tableId,
        url: Feng.ctxPath + '/basicCustomer/list',
        page: true,
        limit: 40,
        height: "full-158",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: BasicCustomer.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BasicCustomer.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        BasicCustomer.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        BasicCustomer.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + BasicCustomer.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BasicCustomer.openEditDlg(data);
        } else if (layEvent === 'delete') {
            BasicCustomer.onDeleteItem(data);
        }
    });

        // Modify parameter status - 修改参数状态
    form.on('switch(status)', function (obj) {
        var id = obj.elem.value;
        var checked = obj.elem.checked ? true : false;

        BasicCustomer.changeGradingstatus(id, checked);
    });
// Modify parameter status
    // 修改参数状态
    form.on('switch(inWarehousing)', function (obj) {
        var id = obj.elem.value;
        var checked = obj.elem.checked ? true : false;

        BasicCustomer.changeGradingPullStoragetaskXlb(id, checked);
    });

    /**
     * Modify parameter status
     * 修改参数状态
     *
     * @param id Parameter id
     * @param checked Whether selected (true,false), selected means enable, unselected means disable
     */
    BasicCustomer.changeGradingPullStoragetaskXlb = function (id, checked) {
        if (checked) {
            var ajax = new $ax(Feng.ctxPath + "/basicCustomer/editItem", function (data) {
                Feng.success("Enable successful!"); // 启用成功!
            }, function (data) {
                Feng.error("Enable failed!"); // 启用失败!
                table.reload(BasicCustomer.tableId);
            });
            ajax.set("inWarehousing", 1);
            ajax.set("id", id);
            ajax.start();
        } else {
            var ajax = new $ax(Feng.ctxPath + "/basicCustomer/editItem", function (data) {
                Feng.success("Disable successful!"); // 禁用成功!
            }, function (data) {
                Feng.error("Disable failed!" + data.responseJSON.message + "!"); // 禁用失败!
                table.reload(BasicCustomer.tableId);
            });
            ajax.set("inWarehousing", 2);
            ajax.set("id", id);
            ajax.start();
        }
    };
    /**
     * 修改参数状态 - Modify parameter status
     *
     * @param id Parameter id
     * @param checked Whether selected (true,false), selected means enable, unselected means disable
     */
    BasicCustomer.changeGradingstatus = function (id, checked) {
        if (checked) {
            var ajax = new $ax(Feng.ctxPath + "/basicCustomer/editItem", function (data) {
                Feng.success("Enable successful!"); // 启用成功!
            }, function (data) {
                Feng.error("Enable failed!"); // 启用失败!
                table.reload(BasicCustomer.tableId);
            });
            ajax.set("status", 1);
            ajax.set("id", id);
            ajax.start();
        } else {
            var ajax = new $ax(Feng.ctxPath + "/basicCustomer/editItem", function (data) {
                Feng.success("Disable successful!"); // 禁用成功!
            }, function (data) {
                Feng.error("Disable failed!" + data.responseJSON.message + "!"); // 禁用失败!
                table.reload(BasicCustomer.tableId);
            });
            ajax.set("status", 2);
            ajax.set("id", id);
            ajax.start();
        }
    };

    // 修改参数状态
    form.on('switch(pullStoragetaskXlb)', function (obj) {
        var id = obj.elem.value;
        var checked = obj.elem.checked ? true : false;

        BasicCustomer.changeGradingPullStoragetaskXlb(id, checked);
    });

    /**
     * 修改参数状态
     *
     * @param id 参数id
     * @param checked 是否选中（true,false），选中就是启用用户，未选中就是禁用用户
     */
    BasicCustomer.changeGradingPullStoragetaskXlb = function (id, checked) {
        if (checked) {
            var ajax = new $ax(Feng.ctxPath + "/basicCustomer/editItem", function (data) {
                 Feng.success("Enable successful!"); // 启用成功!
            }, function (data) {
                Feng.error("Enable failed!"); // 启用失败!
                table.reload(BasicCustomer.tableId);
            });
            ajax.set("pullStoragetaskXlb", 1);
            ajax.set("id", id);
            ajax.start();
        } else {
            var ajax = new $ax(Feng.ctxPath + "/basicCustomer/editItem", function (data) {
                Feng.success("Disable successful!");
            }, function (data) {
                Feng.error("Disable failed!" + data.responseJSON.message + "!"); // 禁用失败!
                table.reload(BasicCustomer.tableId);
            });
            ajax.set("pullStoragetaskXlb", 2);
            ajax.set("id", id);
            ajax.start();
        }
    };

    // 修改参数状态
    form.on('switch(needCheck)', function (obj) {
        var id = obj.elem.value;
        var checked = obj.elem.checked ? true : false;

        BasicCustomer.changeGradingNeedCheck(id, checked);
    });

    /**
     * 修改参数状态
     *
     * @param id 参数id
     * @param checked 是否选中（true,false），选中就是启用用户，未选中就是禁用用户
     */
    BasicCustomer.changeGradingNeedCheck = function (id, checked) {
        if (checked) {
            var ajax = new $ax(Feng.ctxPath + "/basicCustomer/editItem", function (data) {
                Feng.success("Enable successful!"); // 启用成功!
            }, function (data) {
                Feng.error("Enable failed!"); // 启用失败!
                table.reload(BasicCustomer.tableId);
            });
            ajax.set("needCheck", 1);
            ajax.set("id", id);
            ajax.start();
        } else {
            var ajax = new $ax(Feng.ctxPath + "/basicCustomer/editItem", function (data) {
                Feng.success("Disable successful!");
            }, function (data) {
                Feng.error("Disable failed!" + data.responseJSON.message + "!"); // 禁用失败!
                table.reload(BasicCustomer.tableId);
            });
            ajax.set("needCheck", 2);
            ajax.set("id", id);
            ajax.start();
        }
    };

    // 修改参数状态
    form.on('switch(generateTms)', function (obj) {
        var id = obj.elem.value;
        var checked = obj.elem.checked ? true : false;

        BasicCustomer.changeGradingGenerateTms(id, checked);
    });

    /**
     * 修改参数状态
     *
     * @param id 参数id
     * @param checked 是否选中（true,false），选中就是启用用户，未选中就是禁用用户
     */
    BasicCustomer.changeGradingGenerateTms = function (id, checked) {
        if (checked) {
            var ajax = new $ax(Feng.ctxPath + "/basicCustomer/editItem", function (data) {
                Feng.success("Enable successful!"); // 启用成功!
            }, function (data) {
                Feng.error("Enable failed!"); // 启用失败!
                table.reload(BasicCustomer.tableId);
            });
            ajax.set("generateTms", 1);
            ajax.set("id", id);
            ajax.start();
        } else {
            var ajax = new $ax(Feng.ctxPath + "/basicCustomer/editItem", function (data) {
                Feng.success("Disable successful!");
            }, function (data) {
                Feng.error("Disable failed!" + data.responseJSON.message + "!"); // 禁用失败!
                table.reload(BasicCustomer.tableId);
            });
            ajax.set("generateTms", 2);
            ajax.set("id", id);
            ajax.start();
        }
    };
    // 修改参数状态
    form.on('switch(pullYuyueXlb)', function (obj) {
        var id = obj.elem.value;
        var checked = obj.elem.checked ? true : false;

        BasicCustomer.changeGradingGenerateTms(id, checked);
    });

    /**
     * 修改参数状态
     *
     * @param id 参数id
     * @param checked 是否选中（true,false），选中就是启用用户，未选中就是禁用用户
     */
    BasicCustomer.changeGradingGenerateTms = function (id, checked) {
        if (checked) {
            var ajax = new $ax(Feng.ctxPath + "/basicCustomer/editItem", function (data) {
                 Feng.success("Enable successful!"); // 启用成功!
            }, function (data) {
                Feng.error("Enable failed!"); // 启用失败!
                table.reload(BasicCustomer.tableId);
            });
            ajax.set("pullYuyueXlb", 1);
            ajax.set("id", id);
            ajax.start();
        } else {
            var ajax = new $ax(Feng.ctxPath + "/basicCustomer/editItem", function (data) {
                Feng.success("Disable successful!");
            }, function (data) {
                Feng.error("Disable failed!" + data.responseJSON.message + "!"); // 禁用失败!

                table.reload(BasicCustomer.tableId);
            });
            ajax.set("pullYuyueXlb", 2);
            ajax.set("id", id);
            ajax.start();
        }
    };
});
